package com.example.administrator.liangcang.store.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.liangcang.BR;
import com.example.administrator.liangcang.R;
import com.example.administrator.liangcang.callback.LiangCangService;
import com.example.administrator.liangcang.databinding.FragmentBrandBinding;
import com.example.administrator.liangcang.store.bean.brand.BrandJsonEntity;
import com.example.administrator.liangcang.until.BaseApplication;
import com.example.administrator.liangcang.until.CommentRecyclerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class BrandFragment extends Fragment implements Callback<BrandJsonEntity> {
    private FragmentBrandBinding binding;
    private CommentRecyclerAdapter<Object> adapter;
    private Map<Class<?>,Pair<Integer,Integer>> map;
    private BaseApplication application;
    private LiangCangService service;
    private int PAGE = 1;


    public BrandFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_brand, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        application = ((BaseApplication) getActivity().getApplication());
        service = application.getService();
        service.getBrandTotal("Android",20,PAGE,"47D2F0283AE0D2752370C2EC802CA37D%7C291548701493468","1.0").enqueue(this);
        map = new HashMap<>();
        map.put(null, Pair.create(R.layout.store_brand_item, BR.items));
        adapter = new CommentRecyclerAdapter<Object>(getContext(),new ArrayList<>(),map);
        binding.storeBrandRecycler.setAdapter(adapter);
    }

    @Override
    public void onResponse(Call<BrandJsonEntity> call, Response<BrandJsonEntity> response) {
        if (response.body().getData().getItems() != null) {
            adapter.addAll(response.body().getData().getItems());
        }
    }

    @Override
    public void onFailure(Call<BrandJsonEntity> call, Throwable t) {

    }
}
